<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" width="70%" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
                <el-input
                        v-model="form.name"
                        autocomplete="off"
                        placeholder="请输入名称"
                ></el-input>
            </el-form-item>
            <el-form-item label="邮件标题" :label-width="formLabelWidth" prop="title">
                <el-input
                        v-model="form.title"
                        autocomplete="off"
                        placeholder="请输入名称"
                ></el-input>
            </el-form-item>

          <el-form-item label="邮件内容" :label-width="formLabelWidth" prop="content">
            <el-input
                type="textarea"
                v-model="form.content"
                autocomplete="off"
                placeholder="请输入内容"
                :autosize="{ minRows: 10}"
            ></el-input>
            <el-button type="text" @click="preview()">邮件预览</el-button>
<!--            <fortress-wang-editor v-model="form.content"></fortress-wang-editor>-->
          </el-form-item>
            <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
                <el-input
                      type="textarea"
                        v-model="form.remark"
                        autocomplete="off"
                        placeholder="请输入备注"
                ></el-input>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
                <el-switch
                      v-model="form.status"
                      active-color="#13ce66"
                      inactive-color="#ff4949"
                      :active-value="1"
                      :inactive-value="2"
                      active-text="启用"
                      inactive-text="禁用"
                ></el-switch>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
            <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
            <el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
        </div>
      <el-dialog  append-to-body title="邮件内容预览" :visible.sync="previewVisible" :close-on-click-modal="false" >
        <div v-html="viewContent"></div>
        <div slot="footer" class="dialog-footer" >
          <el-button @click="previewVisible = false" size="medium" type="danger">关闭</el-button>
        </div>
      </el-dialog>
    </el-dialog>
</template>
<script>
    import { castleconfigmailcaptchaInfo, castleconfigmailcaptchaSave, castleconfigmailcaptchaEdit } from "@/api/message/mail/castleconfigmailcaptcha.js";


    export default {
        data() {
            return {
              previewVisible: false,
              visible: false,
                form: {
                    content:"您的验证码是:${code}",
                    name:"",
                    title:"",
                    remark:"",
                    status:"2",
                },
                formLabelWidth: "120px",
                rules: {
                  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
                  title: [{ required: true, message: "请输入邮件标题", trigger: "blur" }],
                  content: [{ required: true, message: "请输入邮件内容", trigger: "blur" }],
                },
              viewContent:''
            };
        },
        created() {
        },
        methods: {
          preview(){
            this.previewVisible = true
            this.viewContent = this.form.content.replace("${code}" , 123456)
          },
            init(id) {
                this.resetForm();
                //修改
                if (id) {
                    castleconfigmailcaptchaInfo(id).then((resp) => {
                        if (resp.code == 0) {
                            this.$nextTick(() => {
                                this.form = {
                                    ...this.form,
                                    ...resp.data,
                                };
                            });
                        } else {
                            this.$msg({
                                message: resp.msg,
                                type: "error",
                            });
                        }
                    });
                }
                this.visible = true;
            },
            //提交表单数据
            submitForm() {
                this.$refs["form"].validate((valid) => {
                if (valid) {
                    //修改
                    if (this.form.id) {
                        castleconfigmailcaptchaEdit(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                            this.$emit("refreshData");
                            this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    //新增
                    } else {
                        castleconfigmailcaptchaSave(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                                this.$emit("refreshData");
                                this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
                });
            },
            resetForm() {
                this.form = {
                  content:"您的验证码是:${code}",
                    name:"",
                    title:"",
                    remark:"",
                    status:"2",
                };
                this.$nextTick(() => {
                    if (this.$refs["form"]) {
                        this.$refs["form"].resetFields();
                    }
                });
            },
        },
    };
</script>
<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
        height: auto;
        max-height: 274px;
        padding: 7px;
        overflow: hidden;
        overflow-y: auto;
        font-weight: normal;
    }
</style>
